package com.ncd.form.controller;


import cn.hutool.core.util.ObjectUtil;
import com.ncd.common.annotation.Log;
import com.ncd.common.core.controller.BaseController;
import com.ncd.common.core.domain.AjaxResult;
import com.ncd.common.core.domain.Ztree;
import com.ncd.common.core.page.TableDataInfo;
import com.ncd.common.core.text.Convert;
import com.ncd.common.enums.BusinessType;
import com.ncd.form.domain.FormDict;
import com.ncd.form.service.IFormDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 表单字典Controller
 *
 * @author tree_new_bee
 */
@Controller
@RequestMapping("/form/dict")
public class FormDictController extends BaseController {

    private String prefix = "form/dict";

    @Resource
    private IFormDictService dictService;

    @RequiresPermissions("form:dict:view")
    @GetMapping()
    public String dict()
    {
        return prefix + "/dict";
    }

    /**
     * 查询表单元素字典列表
     */
    @RequiresPermissions("form:dict:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FormDict dict)
    {
        startPage();
        List<FormDict> list = dictService.queryList(dict);
        return getDataTable(list);
    }



    /**
     * 新增表单元素字典
     */
    @GetMapping("/add/{parent}")
    public String add(@PathVariable("parent")Long parent, ModelMap mmap)
    {
        FormDict dict=new FormDict();
        if(parent.longValue()>0){
            dict=dictService.getById(parent);
        }
        mmap.put("dict",dict);
        mmap.put("parent",parent);
        return prefix + "/add";
    }

    /**
     * 新增保存表单元素字典
     */
    @RequiresPermissions("form:dict:add")
    @Log(title = "表单元素字典", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FormDict dict)
    {
        return toAjax(dictService.save(dict));
    }

    /**
     * 修改表单元素字典
     */
    @GetMapping("/edit/{dictId}")
    public String edit(@PathVariable("dictId") Long dictId, ModelMap mmap)
    {
        FormDict dict = dictService.getById(dictId);
        String parentName="";
        if(0!=dict.getDictParent()){
            FormDict parentDict= dictService.getById(dict.getDictParent());
            parentName=parentDict.getDictCode()+"->"+parentDict.getDictTitle();
        }
        String dictLinkCode="";
        if(ObjectUtil.isNotNull(dict.getDictLinkId())){
            FormDict linkDict=dictService.getById(dict.getDictLinkId());
            if(ObjectUtil.isNotNull(linkDict)){
                dictLinkCode=linkDict.getDictCode()+"->"+linkDict.getDictTitle();
            }
        }
        mmap.put("parentName", parentName);
        mmap.put("dictLinkCode", dictLinkCode);
        mmap.put("dict", dict);
        return prefix + "/edit";
    }

    /**
     * 修改保存表单元素字典
     */
    @RequiresPermissions("form:dict:edit")
    @Log(title = "表单元素字典", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FormDict dict)
    {
        if(dict.getDictParent()==dict.getDictId()){
            return AjaxResult.warn("父节点不能是自己");
        }
        return toAjax(dictService.updateById(dict));
    }

    /**
     * 删除表单元素字典
     */
    @RequiresPermissions("form:dict:remove")
    @Log(title = "表单元素字典", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dictService.removeByIds(Convert.toLongList(ids)));
    }


    /**
     * 加载字典树
     */
    @GetMapping("/treeData/{parentId}")
    @ResponseBody
    public List<Ztree> treeData(@PathVariable("parentId") Long parentId)
    {
        return dictService.selectDictTree(parentId);
    }

    @RequiresPermissions("form:dict:view")
    @GetMapping("/list")
    public String list(FormDict formDict,ModelMap mma)
    {
        String dictDataSet="";
        if(ObjectUtil.isNotNull(formDict)){
            dictDataSet=formDict.getDictDataSet();
        }
        mma.put("dictDataSet",dictDataSet);
        return prefix + "/list";
    }


    /**
     * 加载字典树
     */
    @GetMapping("/tableRowHtml")
    @ResponseBody
    public AjaxResult tableRowHtml(Long dictId)
    {
        FormDict dict=dictService.selectDictByParent(dictId,null);
        return AjaxResult.success(dict);
    }


}
